package tree;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * Description:
 * Author:    Dane
 * CreateTime:2024/8/24-上午11:34
 * Since  :   1.0
 * Version:   1.0
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class HeroNode {

    private int no;
    private String name;
    private HeroNode leftNode;
    private HeroNode rightNode;

    public HeroNode(int no, String name) {
        this.no = no;
        this.name = name;
    }

    public void preOrder() {
        System.out.println(this);
        if (this.leftNode != null) {
            this.leftNode.preOrder();
        }
        if (this.rightNode != null) {
            this.rightNode.preOrder();
        }

    }

    public void midOrder() {
        if (this.leftNode != null) {
            this.leftNode.midOrder();
        }
        System.out.println(this);
        if (this.rightNode != null) {
            this.rightNode.midOrder();
        }
    }

    public void postOrder() {
        if (this.leftNode != null) {
            this.leftNode.postOrder();
        }

        if (this.rightNode != null) {
            this.rightNode.postOrder();
        }
        System.out.println(this);

    }

    public HeroNode preSearch(int no) {
        HeroNode result = null;
        if (this.no == no) {
            return this;
        }
        if (this.leftNode != null) {
            result = this.leftNode.preSearch(no);
        }
        if (result != null) {
            return result;
        }
        if (this.rightNode != null) {
            result = this.rightNode.preSearch(no);
        }
        return result;


    }

    public HeroNode midSearch(int no) {
        HeroNode result = null;
        if (this.leftNode != null) {
            result = this.leftNode.midSearch(no);
        }
        if (result != null) {
            return result;
        }
        if (this.no == no) {
            return this;
        }

        if (this.rightNode != null) {
            result = this.rightNode.midSearch(no);
        }
        return result;

    }

    public HeroNode postSearch(int no) {
        HeroNode result = null;
        if (this.leftNode != null) {
            result = this.leftNode.postSearch(no);
        }
        if (result != null) {
            return result;
        }
        if (this.rightNode != null) {
            result = this.rightNode.postSearch(no);
        }
        if (result != null) {
            return result;
        }
        if (this.no == no) {
            result = this;
        }
        return result;

    }

    public void delNo(int no) {
        if (this.leftNode != null && this.leftNode.no == no) {
            this.leftNode = null;

        }
        if (this.rightNode != null && this.rightNode.no == no) {
            this.rightNode = null;

        }
        if (this.leftNode != null) {
            this.leftNode.delNo(no);
        }
        if (this.rightNode != null) {
            this.rightNode.delNo(no);
        }


    }




}
